﻿using System;
using System.ComponentModel;
using System.Globalization;

namespace TWiStEr.Math {
	public sealed class VectorConverter : TypeConverter {
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
			return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
		}
		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
			return ((destinationType == typeof(string)) || base.CanConvertTo(context, destinationType));
		}
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
			if (value == null) {
				throw base.GetConvertFromException(value);
			}
			return Vector.Parse(string.Format("{0}", value));
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
			if ((destinationType != null) && (value is Vector)) {
				Vector vector = (Vector)value;
				if (destinationType == typeof(string)) {
					return vector.ToString(null, culture);
				}
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}
	}
}
